package assignment.ton.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.regex.PatternSyntaxException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;

import assignment.ton.def.Action;
import assignment.ton.def.Class;
import assignment.ton.def.Method;
import assignment.ton.def.Util;

public class MethodFrame extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1057385420534796560L;
	private Class theClass;
	private MainFrame mainFrame;
	private JComboBox methodList;
	private JButton addButton, editButton, deleteButton;
	public MethodFrame(Class cL, MainFrame mF){
		theClass = cL;
		mainFrame = mF;
		this.setTitle("Methods");
		this.addWindowListener(new WindowAdapter(){ //now make it do something more interesting
			public void windowClosing(WindowEvent e){
				quit();
			}
		});
		this.setResizable(false);
		this.setLocationRelativeTo(mainFrame);
		this.setLayout(new GridBagLayout());
		this.setIconImage(new ImageIcon(getClass().getResource("ton_icon.gif")).getImage());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(2,2,2,2);
		String [] data = theClass.getMethodList();
		methodList = new JComboBox(data);
		c.gridx = 0;
		c.gridy = 0;
		this.add(methodList, c);
		c.gridx = 1;
		addButton = new JButton("Add");
		addButton.addActionListener(this);
		this.add(addButton, c);
		c.gridx = 2;
		editButton = new JButton("Edit");
		editButton.addActionListener(this);
		this.add(editButton, c);
		c.gridx = 3;
		deleteButton = new JButton("Delete");
		deleteButton.addActionListener(this);
		this.add(deleteButton,c);
		pack();
	}
	public void quit(){
		this.dispose();
		mainFrame.setAction(Action.CLASSSELECTED);
	}
	public void rebuildList(){
		methodList.removeAllItems();
		String[] param = theClass.getMethodList();
		for(int i = 0; i<param.length; i++){
			methodList.addItem(param[i]);
		}
		pack();
	}
	public void addMethod(Method m){
		theClass.addMethod(m);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		String actionEvent = e.getActionCommand();
		if("Add".equals(actionEvent)){
			Method m = new Method(null, null);
			EditMethod ed = new EditMethod(mainFrame, m, this);
			ed.setVisible(true);
		}else if("Edit".equals(actionEvent)){
			try{
				String a = (String)methodList.getSelectedItem();
				String[] b = a.split(":");
				Method g = theClass.getMethodByName(b[0]);
				EditMethod ed = new EditMethod(mainFrame, g, this);
				ed.setVisible(true);
			}catch(PatternSyntaxException k){
				Util.showError(this, "Quit trying to edit nothing");
			}
		}else if("Delete".equals(actionEvent)){
			try{
				String a = (String)methodList.getSelectedItem();
				String[] b = a.split(":");
				Method g = theClass.getMethodByName(b[0]);
				theClass.removeMethod(g);
				rebuildList();
			}catch(PatternSyntaxException k){
				Util.showError(this, "Quit trying to delete nothing");
			}
		}	
	}
}
